package namespace

import (
	"fmt"
	"os"
	"os/exec"
	"syscall"
)

func main() {
	var (
		err error
	)

	//create process
	cmd := exec.Command("sh")

	//cmd attr
	//syscall.CLONE_NEWPID can let same process have different pid in different pid namespace
	cmd.SysProcAttr = &syscall.SysProcAttr{
		Cloneflags: syscall.CLONE_NEWUTS | syscall.CLONE_NEWPID | syscall.CLONE_NEWIPC |
			syscall.CLONE_NEWUSER | syscall.CLONE_NEWNS | syscall.CLONE_NEWNET,
	}

	//namespace test
	//user
	//ipc
	//pid
	//uts
	//mount CLONE_NEWNS

	//uid and gid is available before kernel 3.19
	//cmd.SysProcAttr.Credential = &syscall.Credential{
	//	Uid: 1,
	//	Gid: 1,
	//}
	cmd.SysProcAttr.UidMappings = []syscall.SysProcIDMap{
		{
			ContainerID: 1234, //new value
			HostID:      0,
			Size:        1,
		},
	}
	cmd.SysProcAttr.GidMappings = []syscall.SysProcIDMap{
		{
			ContainerID: 1234, //new value
			HostID:      0,
			Size:        1,
		},
	}

	cmd.Stdin = os.Stdin
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr

	//start process
	err = cmd.Run()
	if err != nil {
		fmt.Println(err)
		return
	}
}
